#include <iostream>
#include <future>
#include <thread>
#include <chrono>

int Add(int a, int b)
{
    std::cout << "Add() is called" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(3));
    return a+b;
}

void test()
{
    // async参数：
    // std::launch::async -> 任务会在一个新的线程中异步执行
    // std::launch::deferred -> 任务不会立即执行，而是会在调用 future::get 或 future::wait 时才执行
    // 默认策略：std::launch::async | std::launch::deferred
    std::future<int> result = std::async(std::launch::deferred, Add, 3, 7);

    std::this_thread::sleep_for(std::chrono::seconds(1));
    int num = result.get();     // get()执行时才会调用future对象中设置的Add()函数
    std::cout << "Result number of Add() is " << num << std::endl;
}

int main()
{
    test();
    return 0;
}